//
//  M4AboutUsViewController.swift
//  M4
//
//  Created by YuChuang on 2024/8/9.
//

import UIKit
import ActiveLabel
import SafariServices

class M4AboutUsViewController: M4BaseViewController {
    
    @IBOutlet weak var M4Version: UILabel!
    
    @IBOutlet weak var M4CompanyInfo: UILabel!
    
    @IBOutlet weak var M4Emails: UILabel!
    
    @IBOutlet weak var M4ServicePhones: UILabel!
    
    @IBOutlet weak var M4LogoutButton: UIButton!
    
    @IBOutlet weak var M4Label: ActiveLabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        M4NavBarColor = .white
        M4BackIcon = "icon_back_black"
        M4NavTitle(M4Title: "Sobre nosotros", M4NavTitleColor: COLOR_333333)
        M4LogoutButton.layer.cornerRadius = 2
        M4LogoutButton.layer.borderWidth = 0.5
        M4LogoutButton.layer.borderColor = COLOR_CCCCCC.cgColor
        M4LogoutButton.layer.masksToBounds = true
        M4Version.text = "Versión \(Bundle.main.infoDictionary!["CFBundleShortVersionString"] as? String ?? "1.0")"
//        M4CompanyInfo.text = M4ConfigDataManager.m4ConfigShare.m4companyInfo
        M4Emails.text = M4ConfigDataManager.m4ConfigShare.m4email
        let phones = M4ConfigDataManager.m4ConfigShare.m4servicePhone.replacingOccurrences(of: ",", with: "\n\n")
        M4ServicePhones.text = phones
        
        M4ConfigM4Label()
    }
    
    func M4ConfigM4Label() {
        let str1 = "https://webapps.condusef.gob.mx/SIPRES/jsp/home_publico.jsp?idins=16272"
        let str2 = "https://www.blankfeincapitalmx.com"
        M4Label.enabledTypes = [.url]
        M4Label.text = "Para mayores detalles, invitamos a los usuarios a visitar su sitio web oficial \(str1) o nuestro propio sitio web oficial \(str2) para consultar información relevante."
        M4Label.handleURLTap { url in
            self.M4OpenSafari(string: url.absoluteString)
        }
    }
    
    func M4OpenSafari(string: String) {
        if let url = URL(string: string) {
            let safariController = SFSafariViewController(url: url)
            present(safariController, animated: true)
        }
    }
    
    @IBAction func M4LogoutClick(_ sender: UIButton) {
        let alert = UINib(nibName: "M4PublicAlertView", bundle: nil).instantiate(withOwner: self).last as! M4PublicAlertView
        alert.m4updateAlertUI(title: "¿Está seguro de que quiere  abandonarlo ahora?", firstText: "Cancelar", secondText: "Salir")
        alert.M4AlertSecondBlock = {
            self.M4LogoutNet()
        }
        alert.M4AlertViewShow()
        sender.M4EventUnavailable = false
    }
    
    @IBAction func M4ClearAccountClick(_ sender: UIButton) {
        let alert = UINib(nibName: "M4PublicAlertView", bundle: nil).instantiate(withOwner: self).last as! M4PublicAlertView
        alert.m4updateAlertUI(title: "Después de la cancelación, perderá su crédito actual y no podrá utilizar su cuenta, por favor proceda con precaución", firstText: "Cancelar", secondText: "Confirmación")
        alert.M4AlertSecondBlock = {
            self.M4ClearAccountNet()
        }
        alert.M4AlertViewShow()
        sender.M4EventUnavailable = false
    }
    
    private func M4LogoutNet() {
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4logout, .post, params: ["NkY": M4UserDataManager.m4UserShare.M4Token, "Rby": M4UserDataManager.m4UserShare.M4Itemcode])) { model in
            if model.code == 1 {
                M4UserDataManager.m4UserShare.M4Token = ""
                M4UserDataManager.m4UserShare.M4AuditAccount = 0
//                self.navigationController?.popViewController(animated: false)
//                M4BaseViewController.M4CurrentVC()?.tabBarController?.selectedIndex = 0
                /* 退出登陆直接显示登录页面 */
                AppDelegate.M4JumpLoginPage()
            }
            else {
                self.view.makeToast(model.message, position: .center)
            }
        }
    }
    
    private func M4ClearAccountNet() {
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4clearAccount, .post, params: ["HKUdfa": "ESP", "rYqf": M4UserDataManager.m4UserShare.M4Token, "oGdsf": M4UserDataManager.m4UserShare.M4Itemcode])) { model in
            if model.code == 1 {
                M4UserDataManager.m4UserShare.M4Token = ""
                M4UserDataManager.m4UserShare.M4AuditAccount = 0
//                self.navigationController?.popViewController(animated: false)
//                M4BaseViewController.M4CurrentVC()?.tabBarController?.selectedIndex = 0
                /* 退出登陆直接显示登录页面 */
                AppDelegate.M4JumpLoginPage()
            }
            else {
                self.view.makeToast(model.message, position: .center)
            }
        }
    }
    
}
